
#ifndef My_INC_LC_LC_SYS_H_
#define My_INC_LC_LC_SYS_H_

#include "MY_INC/PHY/PHY_Compensation.h"
#include "MY_INC/PHY/PHY_Cordic.h"
#include "MY_INC/PHY/PHY_Filter.h"
#include "MY_INC/PHY/PHY_PID.h"
#include "MY_INC/PHY/PHY_PLL.h"
#include "MY_INC/PHY/PHY_PQ.h"
#include "MY_INC/PHY/PHY_PR.h"
#include "MY_INC/PHY/PHY_Parameter.h"
#include "MY_INC/PHY/PHY_Print.h"
#include "MY_INC/PHY/PHY_RMS.h"
#include "MY_INC/PHY/PHY_SignalAnalysisr.h"
#include "MY_INC/PHY/PHY_TOGI.h"
#include "MY_INC/PHY/PHY_math.h"

#include "MY_INC/LC/LC_ADC.h"
#include "MY_INC/LC/LC_LED.h"
#include "hrtim.h"
#include "stdint.h"

/*========================
 * 枚举定义
 *========================*/

// 设备工作模式

typedef enum
{
    SYS_STATE_INIT = 0U,
    SYS_STATE_SS,
    SYS_STATE_SSC,
    SYS_STATE_WORKING,
} SysState;
// 控制模式
typedef enum
{
    WOCK_MODE_STOP = 0U,
    WOCK_MODE_SOURCE,
    WOCK_MODE_LOAD
} WockMode;
// 控制模式
typedef enum
{
    DEVICE_MODE_STOP = 0U,
    DEVICE_MODE_OPEN,
    DEVICE_MODE_CURRENT,
    DEVICE_MODE_VOLTAGE,
    DEVICE_MODE_POWER,
    DEVICE_MODE_IMPEDANCE
} DeviceMode;

// 控制环路模式
typedef enum
{
    LOOP_MODE_OPEN = 0U,
    LOOP_MODE_INV_VOLTAGE,
    LOOP_MODE_INV_CURRENT,
    LOOP_MODE_REC_VOLTAGE,
    LOOP_MODE_REC_CURRENT
} LoopMode;

// 环路运行状态
typedef enum
{
    STATE_STOP = 0U,
    STATE_IDLE,
    STATE_WAIT_LOCK,
    STATE_WAIT_CROSS_ZERO,
    STATE_RUN
} LoopState;

// 外部事件
typedef enum
{
    EVENT_LOOP_NONE = 0U,
    EVENT_LOOP_STOP,
    EVENT_LOOP_ENABLE
} LoopEvent;

// 保护状态
typedef enum
{
    PROT_STATE_NORMAL = 0U,
    PROT_STATE_GRID_OVER_VOLTAGE,
    PROT_STATE_INDUCTOR_OVER_CURRENT,
    PROT_STATE_BUS_UNDER_VOLTAGE,
    PROT_STATE_BUS_OVER_VOLTAGE,
    PROT_STATE_MULTIPLE_FAULTS
} ProtectionState;

// 设备状态
typedef enum
{
    DEVICE_STATE_NORMAL = 0U,
    DEVICE_STATE_FAULT,
    DEVICE_STATE_HALT
} DeviceState;

// 标志状态
typedef enum
{
    Nonactivated = 0U,
    Activated = 1U
} ActivatedState;
/*========================
 * 常量定义
 *========================*/
#define STATE_TRACE_SIZE 32

/*========================
 * 数据结构定义
 *========================*/

// 控制信号结构体（带实际值与参考值）

// 控制参数结构体
typedef struct
{
    uint8_t  lock_count;
    uint16_t lock_timeout;
    int16_t  phase_count;
    float    Phaseshift_angle; // 移相角，弧度制  ,正为感性负载，负为容性负载

    float theta; // PLL 角度
    float sin_val;
    float cos_val;
    float vo_ref;     // 网侧电压目标值，有效值
    float il_ref;     // 电感电流目标值，有效值
    float udc_ref;    // 母线电压
    float pe_ref;     // 有功功率，参考值
    float qe_ref;     // 无功功率，参考值
    float vo_actual;  // 网侧电压
    float il_actual;  // 电感电流
    float udc_actual; // 母线电压

    /*交流测相关*/
    PR_Diff_Typedef *pr_i[Harmonic_Order];
    PR_Diff_Typedef *pr_v[Harmonic_Order];

    /*锁相相关*/
    PLL_Typedef  *pll;
    TOGI_Typedef *togi_v;
    TOGI_Typedef *togi_i;

    /*可变补偿相关*/
    VariableStep_Typedef *iref;
    VariableStep_Typedef *vref;
    VariableStep_Typedef *udcref;

    /*直流母线相关*/
    AntiWindupPID__Typedef *pi_udc; // 电压PI
    FilterTrap_Typedef     *trap_udc;

    /*有效值相关*/
    SignalAnalysisr_Typedef *SignalAnalysisr_v;
    SignalAnalysisr_Typedef *SignalAnalysisr_i;

    /*功率相关*/
    PQ_Typedef             *pq;    // 有功无功功率计算
    AntiWindupPID__Typedef *pi_pe; // 有功PI
    AntiWindupPID__Typedef *pi_qe; // 无功PI
    VariableStep_Typedef   *peref;
    VariableStep_Typedef   *qeref;

    /*时间系数*/
    const float pr_ts;
    const float pll_ts;
    const float togi_v_ts;
    const float togi_i_ts;
    const float variableStep_ts;
    const float pid_ts;
    const float filtertrap_ts;
    const float signalanalysisr_ts;
    const float pq_ts;

} LoopParameter;

// PWM控制配置
typedef struct
{
    float               mod_wave;                 // 调制波
    ActivatedState      pwm_flag : 1;             // PWM标志
    ActivatedState      pwm_en_flag : 1;          // 闸PWM允许标志位
    ActivatedState      pwm_gpio_flag : 1;        // 闸门标志位
    uint32_t            pwm_width[BRIDGE_NUMBER]; // PWM宽度数组
    const uint32_t      timer[BRIDGE_NUMBER];     // 所需HRTIM定时器
    const uint32_t      output_p[BRIDGE_NUMBER];  // 所需HRTIM定时器通道
    const uint32_t      output_n[BRIDGE_NUMBER];
    const GPIO_TypeDef *pwm_enable_gpio_port; // PWM使能GPIO端口
    const uint32_t      pwm_enable_pin;       // PWM使能GPIO引脚
} Pwm;
// 环路状态机结构
typedef struct
{
    ActivatedState init_flag : 1; // 是否被初始化过
    LoopState      current_state;
    LoopEvent      event;
    ActivatedState start_flag;
    ActivatedState stop_flag;
} LoopStateMachine;

// 系统保护与状态监控结构
typedef struct
{
    ActivatedState is_grid_over_voltage : 1;
    ActivatedState is_inductor_over_current : 1;
    ActivatedState is_bus_under_voltage : 1;
    ActivatedState is_bus_over_voltage : 1;
    ActivatedState fault_detected : 1;

    float grid_over_voltage_threshold; // 网侧电压超压阈值
    float il_over_current_threshold;   // 电感电流阈值
    float bus_over_voltage_threshold;  // 母线电压超压阈值
    float bus_under_voltage_threshold; // 母线电压超压阈值

    uint8_t         fault_count;
    uint8_t         fault_time;
    DeviceState     device_state;
    ProtectionState protection_state;
    ProtectionState error_code;
    ProtectionState state_trace[STATE_TRACE_SIZE];
    uint8_t         state_trace_index;
} Monitor;

// 中断系统（环路 + 保护 + PWM）
typedef struct
{
    uint16_t adc1_res[ADC1_SIZE];
    uint16_t adc2_res[ADC2_SIZE];
    uint16_t adc3_res[ADC3_SIZE];
    uint16_t adc4_res[ADC4_SIZE];
    uint16_t adc5_res[ADC5_SIZE];
} ADC;

typedef struct
{
    ActivatedState task_20us_flag : 1;
    ActivatedState task_40us_flag : 1;
    ActivatedState task_100us_flag : 1;
    ActivatedState task_100ms_flag : 1;
    ActivatedState task_400ms_flag : 1;
    uint8_t        task_20us_count;
    uint8_t        task_40us_count;
    uint8_t        task_100us_count;
    uint16_t       task_100ms_count;
    uint16_t       task_400ms_count;
} Task;
typedef struct InterruptController
{
    Monitor          monitor; // 保护状态机
    LoopStateMachine loop_sm; // 环路状态机
    Pwm              pwm;     // PWM控制
    LoopParameter    param;   // 环路参数
    ADC              adc;
    LoopMode         loop_mode; // 环路模式选择变量
} InterruptController;

// AC负载系统主结构体
typedef struct
{
    SysState            sys_state;   // 系统状态
    WockMode            wock_mode;   // 工作模式选择
    DeviceMode          device_mode; // 源载工作模式选择
    ActivatedState      switch_flag : 1;
    Task                task;                          // 分时函数
    InterruptController controller[CONTROLLER_NUMBER]; // 0-逆变器侧控制  1- 整流器侧控制
} System;

#endif /* INC_FLIT_H_ */
